﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblCurrencyDAL : ImtblCurrency
    {
        HKEA_CMSEntities model;
        mtblCurrency currency;
        public mtblCurrencyDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblCurrencyDAL GetInstance()
        {
            return new mtblCurrencyDAL();
        }

        public int InsertENT(mtblCurrency ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblCurrencies.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblCurrency> GetListByENT(mtblCurrency ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblCurrencies.Where(a => a.CurrencyUID > 0);
            if (ent.CurrencyUID > 0)
                result = result.Where(a => a.CurrencyUID == ent.CurrencyUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblCurrency> GetListByENT(mtblCurrency ent)
        {
            var result = model.mtblCurrencies.Where(a => a.CurrencyUID > 0);
            if (ent.CurrencyUID > 0)
                result = result.Where(a => a.CurrencyUID == ent.CurrencyUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblCurrency ent)
        {
            var result = model.mtblCurrencies.Where(a => a.CurrencyUID > 0);
            if (ent.CurrencyUID > 0)
                result = result.Where(a => a.CurrencyUID == ent.CurrencyUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblCurrency ent)
        {
            currency = model.mtblCurrencies.Where(a=>a.CurrencyUID==ent.CurrencyUID).FirstOrDefault();
            if (currency != null)
            {
                currency.Currency_Chi = ent.Currency_Chi;
                currency.Currency_Eng = ent.Currency_Eng;
                currency.Currency_Sign = ent.Currency_Sign;
                currency.Deleted = ent.Deleted;
                currency.LastModifyBy = ent.LastModifyBy;
                currency.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblCurrency ent)
        {
            currency = model.mtblCurrencies.Where(a => a.CurrencyUID == ent.CurrencyUID).FirstOrDefault();
            if (currency != null)
            {
                model.mtblCurrencies.DeleteObject(currency);
            }
            return model.SaveChanges();
        }
    }
}
